﻿package UIComponents
{
	import Classes.GameUtils.MyTipSprite;
	import Classes.GameUtils.ToolTip_Sprite;
	
	import MyComponent.MyButton;
	
	import fl.events.ComponentEvent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.filters.ColorMatrixFilter;
	import flash.text.TextField;
	
	public class ListCollectSuit extends Sprite
	{
		public var collectIcon:Sprite;
		public var exchange_items_sp:IconContainer;
		public var exchangeBtn:MyButton;
		private var imission:IMission;
		public var _dhMb:DisplayObject;
		public function ListCollectSuit()
		{
			init();
		}
		
		private function init():void
		{
			collectIcon=this.getChildByName("exchangeIcon") as Sprite;
			this._dhMb=this.getChildByName("dhMb");
			exchange_items_sp=this.getChildByName("exchange_items") as IconContainer;
			exchangeBtn=this.getChildByName("exchangeBtn") as MyButton;
			exchangeBtn.addEventListener(ComponentEvent.BUTTON_DOWN,exchangeItem);
			this.collectIcon.mouseChildren=false;
		}
		
		private function exchangeItem(e:ComponentEvent):void
		{
			this.imission.finishHandler();
		}
		
		private function getFrameCollectByIndex(index:int):FrameCollectItem
		{
			if(index<0 || index>4)throw new Error("索引出错");
			return (this.getChildByName("collectItem_"+(index+1))) as FrameCollectItem;
		}
		
		public function setData(imission:IMission):void
		{
			var btnFlag:Boolean=true;
			this.imission=imission;
			this.reset();
			this.setCollectIcon(imission.missionShowIcon);
			var conditionCountsAry:Array=imission.conditionCountsAry;
			var rewardCountsAry:Array=imission.rewardCountsAry;
			this.setExchangePic(rewardCountsAry[0]);
			
			for(var i:int=0;i<5;i++)
			{
				var frameCollectSuit:FrameCollectItem=this.getFrameCollectByIndex(i);
				frameCollectSuit._mb.visible=true;
				var obj:Object=conditionCountsAry[i];
				if(null!=obj)
				{
					var tmpFlag:Boolean=frameCollectSuit.setFrameData(obj);
					if(!tmpFlag)
					{
						btnFlag=tmpFlag;
					}
				}
			}
			if(!btnFlag)
			{
				this.exchangeBtn.enabled=false;
				
				var matrix:Array = new Array();
				
				matrix = matrix.concat([0.3086, 0.6094, 0.082, 0, 0]); // red
				
				matrix = matrix.concat([0.3086, 0.6094, 0.082, 0, 0]); // green
				
				matrix = matrix.concat([0.3086, 0.6094, 0.082, 0, 0]); // blue
				
				matrix = matrix.concat([0, 0, 0, 1, 0]); // alpha
				
				var gray:ColorMatrixFilter = new ColorMatrixFilter(matrix);
				
				var filtersArray:Array = new Array(gray);
				this.exchangeBtn.filters=filtersArray;
			}else{
				this.exchangeBtn.enabled=true;
				this.exchangeBtn.filters=null;
			}
		}
		
		private function setExchangePic(value:DisplayObject):void
		{
			this._dhMb.visible=false;
			this.exchange_items_sp.addPic(value,value["displayViewIconName"]);
		}
		private function setCollectIcon(value:DisplayObject):void
		{
			var tips:String=this.imission.missionName+"："+this.imission.missionDescription;
			this.collectIcon.addChild(value);
			ToolTip_Sprite.getInstance().setTip(collectIcon,new MyTipSprite(tips),ToolTip_Sprite.DIRECTION_LEFT);
			value.scaleX=0.4;
			value.scaleY=0.4;
		}
		
		private function reset():void
		{
			while(this.collectIcon.numChildren>0) this.collectIcon.removeChildAt(0);
		}
	}
}